<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品统计 - 管理员后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 保持原有样式不变 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #2c3e50;
            --secondary: #34495e;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --dark: #2c3e50;
            --success: #2ecc71;
            --warning: #f39c12;
            --info: #3498db;
            --gray: #95a5a6;
            --light-gray: #f5f7fa;
            --border: #e0e6ed;
            --card-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            --hover-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
        }

        body {
            background-color: var(--light-gray);
            color: #333;
            overflow-x: hidden;
        }

        .admin-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .admin-sidebar {
            width: 250px;
            background: var(--primary);
            color: white;
            padding: 20px 0;
            transition: all 0.3s ease;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
        }

        .admin-logo {
            text-align: center;
            padding: 20px 0;
            border-bottom: 1px solid var(--secondary);
            margin-bottom: 20px;
        }

        .admin-logo h2 {
            font-size: 1.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .admin-menu {
            list-style: none;
            padding: 0;
        }

        .admin-menu li a {
            display: flex;
            align-items: center;
            padding: 15px 30px;
            color: var(--light);
            text-decoration: none;
            transition: all 0.3s;
            border-left: 4px solid transparent;
            font-size: 1rem;
        }

        .admin-menu li a:hover,
        .admin-menu li a.active {
            background: var(--secondary);
            border-left: 4px solid var(--accent);
        }

        .admin-menu li a i {
            width: 25px;
            text-align: center;
            margin-right: 12px;
            font-size: 1.1rem;
        }

        /* 主内容区样式 */
        .admin-content {
            flex: 1;
            padding: 30px;
            margin-left: 250px;
            transition: margin-left 0.3s;
        }

        .admin-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid var(--border);
        }

        .admin-header h1 {
            font-size: 1.8rem;
            color: var(--dark);
        }

        .admin-header h1 span {
            color: var(--accent);
        }

        .admin-header .date-info {
            font-size: 1.1rem;
            color: var(--gray);
            background: var(--light);
            padding: 8px 15px;
            border-radius: 30px;
            box-shadow: var(--card-shadow);
        }

        /* 商品统计特定样式 */
        .product-management-container {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: var(--card-shadow);
        }

        .product-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }

        .product-header h2 {
            color: var(--dark);
            font-size: 1.5rem;
        }

        .product-actions {
            display: flex;
            gap: 15px;
        }

        .product-actions button {
            background: var(--accent);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .product-actions button:hover {
            background: var(--secondary);
        }

        .product-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .product-table th, .product-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }

        .product-table th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: var(--dark);
        }

        .product-table tr:hover {
            background-color: var(--light-gray);
        }

        .product-image {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 4px;
        }

        .product-status {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.85rem;
            font-weight: 500;
        }

        .status-available {
            background-color: rgba(46, 204, 113, 0.1);
            color: var(--success);
        }

        .status-sold {
            background-color: rgba(231, 76, 60, 0.1);
            color: var(--accent);
        }

        .status-pending {
            background-color: rgba(241, 196, 15, 0.1);
            color: var(--warning);
        }

        .product-actions-cell {
            height: 100px;
            gap: 10px;
        }

        .action-btn {
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.85rem;
            border: none;
            transition: all 0.2s;
        }

        .view-btn {
            background-color: var(--success);
            color: white;
        }

        .view-btn:hover {
            background-color: #27ae60;
        }

        .search-bar {
            display: flex;
            margin-bottom: 20px;
            gap: 10px;
        }

        .search-bar input {
            flex: 1;
            padding: 10px 15px;
            border: 1px solid var(--border);
            border-radius: 4px;
            font-size: 1rem;
        }

        .search-bar button {
            background: var(--accent);
            color: white;
            border: none;
            padding: 0 20px;
            border-radius: 4px;
            cursor: pointer;
        }

        .filter-bar {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }

        .filter-bar select {
            padding: 8px 15px;
            border: 1px solid var(--border);
            border-radius: 4px;
            background: white;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 5px;
        }

        .pagination button {
            padding: 8px 12px;
            border: 1px solid var(--border);
            background: white;
            cursor: pointer;
            border-radius: 4px;
        }

        .pagination button.active {
            background: var(--accent);
            color: white;
            border-color: var(--accent);
        }

        .pagination button:hover:not(.active) {
            background: var(--light-gray);
        }

        .product-title {
            font-weight: 600;
            color: var(--dark);
            margin-bottom: 5px;
        }

        .product-desc {
            color: var(--gray);
            font-size: 0.9rem;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
    </style>
</head>
<body>
<div class="admin-container">
    <!-- 侧边栏 -->
    <div class="admin-sidebar">
        <div class="admin-logo">
            <h2><i class="fas fa-user-cog"></i> 管理员后台</h2>
        </div>
        <ul class="admin-menu">
            <li><a href="${pageContext.request.contextPath}/dashboard"
                   class="${activeTab == 'dashboard' ? 'active' : ''}"><i class="fas fa-chart-bar"></i> 数据统计</a></li>
            <li><a href="${pageContext.request.contextPath}/userManage"
                   class="${activeTab == 'user' ? 'active' : ''}"><i class="fas fa-users"></i> 用户统计</a></li>
            <li><a href="${pageContext.request.contextPath}/productManage"
                   class="${activeTab == 'product' ? 'active' : ''}"><i class="fas fa-box"></i> 商品统计</a></li>
            <li><a href="${pageContext.request.contextPath}/orderManege"
                   class="${activeTab == 'order' ? 'active' : ''}"><i class="fas fa-shopping-cart"></i> 订单统计</a></li>
            <li><a href="${pageContext.request.contextPath}/logout" id="logoutLink">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a></li>
        </ul>
    </div>

    <!-- 主内容区 -->
    <div class="admin-content">
        <div class="admin-header">
            <h1>商品统计</h1>
            <div class="date-info">
                <i class="fas fa-calendar-alt"></i> <span id="current-date"></span>
            </div>
        </div>

        <div class="product-management-container">
            <div class="product-header">
                <h2>商品列表</h2>
                <div class="product-actions">
                    <button id="exportBtn">
                        <i class="fas fa-file-export"></i> 导出数据
                    </button>
                </div>
            </div>

            <form id="searchForm" action="${pageContext.request.contextPath}/productManage" method="get">
                <div class="filter-bar">
                    <select name="categoryId" id="categoryFilter">
                        <option value="">所有分类</option>
                        <c:forEach items="${categories}" var="category">
                            <option value="${category.categoryId}" ${param.categoryId == category.categoryId ? 'selected' : ''}>
                                    ${category.categoryName}
                            </option>
                        </c:forEach>
                    </select>
                    <select name="status" id="statusFilter">
                        <option value="">所有状态</option>
                        <option value="1" ${param.status == '1' ? 'selected' : ''}>在售</option>
                        <option value="2" ${param.status == '2' ? 'selected' : ''}>已售</option>
                        <option value="0" ${param.status == '0' ? 'selected' : ''}>下架</option>
                    </select>
                    <select name="sort" id="sortFilter">
                        <option value="newest" ${param.sort == 'newest' ? 'selected' : ''}>最新上架</option>
                        <option value="price_asc" ${param.sort == 'price_asc' ? 'selected' : ''}>价格从低到高</option>
                        <option value="price_desc" ${param.sort == 'price_desc' ? 'selected' : ''}>价格从高到低</option>
                    </select>
                </div>

                <div class="search-bar">
                    <input type="text" name="keyword" placeholder="搜索商品名称、描述..." value="${param.keyword}">
                    <button type="submit"><i class="fas fa-search"></i></button>
                </div>
            </form>

            <table class="product-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>商品图片</th>
                    <th>商品信息</th>
                    <th>分类</th>
                    <th>价格</th>
                    <th>原价</th>
                    <th>成色</th>
                    <th>状态</th>
                    <th>上架时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${products}" var="product">
                    <tr>
                        <td>${product.productId}</td>
                        <td>
                            <img src="${pageContext.request.contextPath}/Market/${product.imageUrl}" alt="商品图片"
                                 onerror="this.src='${pageContext.request.contextPath}/Market/default-product.webp'"
                                 class="product-image">
                        </td>
                        <td>
                            <div class="product-title">${product.title}</div>
                            <div class="product-desc">${product.description}</div>
                        </td>
                        <td>
                            <c:forEach items="${categories}" var="category">
                                <c:if test="${category.categoryId == product.categoryId}">
                                    ${category.categoryName}
                                </c:if>
                            </c:forEach>
                        </td>
                        <td>¥<fmt:formatNumber value="${product.price}" pattern="0.00"/></td>
                        <td>¥<fmt:formatNumber value="${product.originalPrice}" pattern="0.00"/></td>
                        <td>${product.conditionLevel}成新</td>
                        <td>
                            <span class="product-status
                                ${product.status == 1 ? 'status-available' :
                                  product.status == 2 ? 'status-sold' : 'status-pending'}">
                                    ${product.status == 1 ? '在售' :
                                            product.status == 2 ? '已售' : '下架'}
                            </span>
                        </td>
                        <td><fmt:formatDate value="${product.createTime}" pattern="yyyy/MM/dd"/></td>
                        <td class="product-actions-cell">
                            <button class="action-btn view-btn" onclick="viewProduct(${product.productId})">
                                <i class="fas fa-eye"></i> 查看
                            </button>
                        </td>
                    </tr>
                </c:forEach>
                <c:if test="${empty products}">
                    <tr>
                        <td colspan="10" style="text-align: center;">没有找到符合条件的商品</td>
                    </tr>
                </c:if>
                </tbody>
            </table>

            <div class="pagination">
                <%-- 上一页按钮 --%>
                <c:if test="${currentPage > 1}">
                    <button onclick="goToPage(1)"><i class="fas fa-angle-double-left"></i></button>
                    <button onclick="goToPage(${currentPage - 1})"><i class="fas fa-angle-left"></i></button>
                </c:if>
                <c:if test="${currentPage <= 1}">
                    <button disabled><i class="fas fa-angle-double-left"></i></button>
                    <button disabled><i class="fas fa-angle-left"></i></button>
                </c:if>

                <%-- 页码按钮 --%>
                <c:forEach begin="1" end="${totalPages}" var="pageNum">
                    <button class="${currentPage == pageNum ? 'active' : ''}"
                            onclick="goToPage(${pageNum})">${pageNum}</button>
                </c:forEach>

                <%-- 下一页按钮 --%>
                <c:if test="${currentPage < totalPages}">
                    <button onclick="goToPage(${currentPage + 1})"><i class="fas fa-angle-right"></i></button>
                    <button onclick="goToPage(${totalPages})"><i class="fas fa-angle-double-right"></i></button>
                </c:if>
                <c:if test="${currentPage >= totalPages}">
                    <button disabled><i class="fas fa-angle-right"></i></button>
                    <button disabled><i class="fas fa-angle-double-right"></i></button>
                </c:if>
            </div>
        </div>
    </div>
</div>

<script>
    // 显示当前日期和星期
    function updateCurrentTime() {
        const now = new Date();
        const options = {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            weekday: 'long',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false
        };
        const dateTimeStr = now.toLocaleString('zh-CN', options);
        document.getElementById('current-date').textContent = dateTimeStr;
    }

    updateCurrentTime();
    setInterval(updateCurrentTime, 1000);

    // 查看商品详情功能
    function viewProduct(productId) {
        window.location.href = '${pageContext.request.contextPath}/product/detail?id=' + productId;
    }

    function goToPage(pageNum) {
        const form = document.getElementById('searchForm');
        const keyword = '${keyword}';
        const categoryId = document.getElementById('categoryFilter').value;
        const status = document.getElementById('statusFilter').value;
        const sort = document.getElementById('sortFilter').value;

        let url = '${pageContext.request.contextPath}/productManage?page=' + pageNum;

        if (keyword) url += '&keyword=' + encodeURIComponent(keyword);
        if (categoryId) url += '&categoryId=' + categoryId;
        if (status) url += '&status=' + status;
        if (sort) url += '&sort=' + sort;

        window.location.href = url;
    }

    // 导出数据按钮事件
    document.getElementById('exportBtn').addEventListener('click', function () {
        const keyword = '${keyword}';
        const categoryId = document.getElementById('categoryFilter').value;
        const status = document.getElementById('statusFilter').value;
        const sort = document.getElementById('sortFilter').value;
        const pageSize = '${pageSize}';
        const currentPage = '${currentPage}';

        let url = '${pageContext.request.contextPath}/product/export';
        let params = [];

        if (keyword) params.push('keyword=' + encodeURIComponent(keyword));
        if (categoryId) params.push('categoryId=' + categoryId);
        if (status) params.push('status=' + status);
        if (sort && sort !== 'newest') params.push('sort=' + sort);
        if (pageSize && pageSize !== '10') params.push('pageSize=' + pageSize);
        if (currentPage && currentPage !== '1') params.push('page=' + currentPage);

        if (params.length > 0) {
            url += '?' + params.join('&');
        }

        // 使用新窗口打开导出请求
        window.open(url, '_blank');
    });

    // 筛选条件变化自动提交表单
    document.getElementById('categoryFilter').addEventListener('change', function () {
        document.getElementById('searchForm').submit();
    });
    document.getElementById('statusFilter').addEventListener('change', function () {
        document.getElementById('searchForm').submit();
    });
    document.getElementById('sortFilter').addEventListener('change', function () {
        document.getElementById('searchForm').submit();
    });
</script>
</body>
</html>